//
//  FGVoucherDetailViewController.m
//  Mi6App
//
//  Created by Mr.Lemon on 7/25/13.
//  Copyright (c) 2013 4G Secure. All rights reserved.
//

#import "FGVoucherDetailViewController.h"

@interface FGVoucherDetailViewController ()

@end

@implementation FGVoucherDetailViewController

int days, hours, minutes, seconds;
int secondsLeft;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)bindVoucherInfoToView
{
    // Do any additional setup after loading the view from its nib.
    NSString *url= @"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSiHvXd7wotCSkYhsBq3-Q2h5lOyyyTO3cyXdv46iAvGiWLHM0r";
    
    [FGBaseViewController showLoadingIndicator];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    //[request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    
    [self.imgVoucher setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        
        [self.imgVoucher setImage:image];
        [FGBaseViewController hideLoadingIndicator];
        
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
        [self.imgVoucher setImage:[UIImage imageNamed:@"notfound.png"]];
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

   // self.title = LocalizedString("Voucher Detail");
    [self bindVoucherInfoToView];
    
}

-(void)viewDidAppear:(BOOL)animated
{
    timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) updateCountdown
{
    
    NSString *dateString = @"14-08-2013";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSDate *dateFromString = [[NSDate alloc] init];
    // voila!
    dateFromString = [dateFormatter dateFromString:dateString];
    
    
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    
    
    NSDateComponents *componentsHours = [calendar components:NSHourCalendarUnit fromDate:now];
    NSDateComponents *componentMint = [calendar components:NSMinuteCalendarUnit fromDate:now];
    NSDateComponents *componentSec = [calendar components:NSSecondCalendarUnit fromDate:now];
    
    
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *componentsDaysDiff = [gregorianCalendar components:NSDayCalendarUnit
                                                                fromDate:now
                                                                  toDate:dateFromString
                                                                 options:0];
    
    
    
    NSString *daysRemain=[NSString stringWithFormat:@"%02d",componentsDaysDiff.day];
    NSString *hourRemain=[NSString stringWithFormat:@"%02d",(24-componentsHours.hour)];
    NSString *minsReamain=[NSString stringWithFormat:@"%02d",(60-componentMint.minute)];
    NSString *secondsRemain=[NSString stringWithFormat:@"%02d",(60-componentSec.second)];
    
    self.lblCountDownTime.text=[NSString stringWithFormat:@"%@d : %@h",daysRemain,hourRemain];
}

- (IBAction)onBtnPassbookClick:(id)sender {
}

- (IBAction)onBtnShareClick:(id)sender {
    
    [SHKFacebook shareText:@"Share"];
}

- (IBAction)onBtnRedemClick:(id)sender {
    
}

- (IBAction)onBtnMapClick:(id)sender {
    
    FGMapViewController *viewController=[[FGMapViewController alloc] initWithNibName:@"FGMapViewController" bundle:nil];
    [self.navigationController pushViewController:viewController animated:YES];
}
- (void)viewDidUnload {
    
    [self setLblCountDownTime:nil];
    [self setImgVoucher:nil];
    [super viewDidUnload];
}
@end
